package com.sys.web.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.users.pojo.AUsers;
import com.sys.users.service.IUsersDbService;


/**
 * 	管理员首页操作
 * 	@author 朱明宇
 */
@Controller
@RequestMapping("/back/users/")
public class UsersBackController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;

	/**
	 * 用户列表
	 * @return
	 */
	@RequestMapping("/usersList")
	public String usersList(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		/*-------自定义搜索条件-------*/
		String userType = request.getParameter("userType");
		if(userType == null)
		{
			userType = "" ;
		}
		condMap.put("userType", userType);
		request.setAttribute("userType", userType);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AUsers> response = this.usersDbService.findCondListUsersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		model.addAttribute("response", response.toJSON());
		return "/back/users/usersList";
	}
	
	/**
	 * 打开添加用户页面
	 * @return
	 */
	@RequestMapping("/usersInsert")
	public String usersInsert(HttpServletRequest request,String updateFlag,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsert--");
		return "/back/users/usersInsert";
	}
	
	/**
	 * 添加用户提交操作
	 * @return
	 */
	@RequestMapping(value = "/usersInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String usersInsertSubmit(HttpServletRequest request, String birthdayStr , AUsers users, Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
		/* 为对象赋值 */
		users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
		/* 为对象赋值 */
		/* 保存结果 */
		ApiResponse response = this.usersDbService.saveOneUsersService(users);
		ConstatFinalUtil.SYS_LOGGER.info("--用户添加返回结果:{}--",response.toJSON());
		return response.toJSON().toJSONString();
	}
	
	/**
	 * 用户查看
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(String id,Model model,HttpServletRequest request)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInfo--");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<AUsers> response = this.usersDbService.findOneUsersService(condMap);
		/* 存储request */
		model.addAttribute("response", response.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/users/usersUpdate";
		}
		return "/back/users/usersInfo";
	}
	
	/**
	 * 添加用户提交操作
	 * @return
	 */
	@RequestMapping(value = "/usersUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String usersUpdateSubmit(HttpServletRequest request, Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if(id == null)
		{
			id = "" ; 
		}
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<AUsers> response = this.usersDbService.findOneUsersService(condMap);
		AUsers users = response.getDataOneJava() ; 
		/* 存储request */
		model.addAttribute("response", response.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			String status = request.getParameter("status");
			String userType = request.getParameter("userType");
			String discount = request.getParameter("discount");
			String birthdayStr = request.getParameter("birthdayStr");
			
			users.setStatus(Byte.valueOf(status));
			users.setUserType(Byte.valueOf(userType));
			users.setDiscount(Double.valueOf(discount));
			users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
			
			/* 为对象赋值 */
			users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
			
			users.setUpdateTime(new Date());
			
			ApiResponse<Object> apiResponse = this.usersDbService.updateOneUsersService(users);
			return apiResponse.toJSON().toJSONString();
		}
		return response.toJSON().toJSONString();
	}
}